<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="@{/}">
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body layui-form">
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <input type="text" id="groupName" name="groupName" class="layui-input" placeholder="请输入论坛
                   名称" autocomplete="off">
                </div>
                <button id = "concertBtnSearch" class="layui-btn layui-btn-md layui-btn-normal" data-type="reload" lay-filter="queryGroup">查询</button>
            </div>
            <table class="layui-table" id="articleTable" lay-filter="articleTable"></table>
        </div>
    </div>
</div>
</body>

<script type="text/html" id="tableBar">
    {{#  if(d.status == 0){ }}
    <a class="layui-btn layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">审核</a>
    {{#  }  }}
    <a class="layui-btn layui-btn-warm layui-btn-sm" lay-event="del">删除</a>
</script>
<script type="text/javascript" th:src="@{/static/layui/layui.js}"></script>
<script type="text/javascript" th:src="@{/static/js/common.js}"></script>
<script type="text/javascript" th:src="@{/static/js/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/static/js/admin.js}"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        table.render({
            elem: '#articleTable',
            url: base_server + 'forum/query',
            method: 'get',
            page: true,
            cellMinWidth: 100,
            cols: [
                [
                    {field: 'id', title: '编号'},
                    {field: 'type', title: '类型'},
                    {field: 'name', title: '论坛名称'},
                    {field: 'tel', title: '联系方式'},
                    {field: 'addr', title: '集合地'},
                    {field: 'route', title: '路线'},
                    {field:'status', title: '状态'
                        ,templet: function(d){
                            if (d.status == 0)
                                return "未审核"
                            else
                                return "已审核"
                        }
                    },
                    {align: 'center', toolbar: '#tableBar', title: '操作', unresize: true, width: 200}
                ]
            ],
            id: 'pageReload',
            skin: 'line',
            size: 'lg'
        });

        // 搜索按钮点击事件
        $('#concertBtnSearch').click(function () {
            var name = $('#groupName').val();
            console.log(name)
            //暴力重新渲染一遍
            table.render({
                elem: '#articleTable',
                url: base_server + 'forum/queryList?name=' + name.replace(/\s/g,""),
                method: 'get',
                page: true,
                cellMinWidth: 100,
                cols: [
                    [
                        {field: 'id', title: '编号'},
                        {field: 'type', title: '类型'},
                        {field: 'name', title: '论坛名称'},
                        {field: 'tel', title: '联系方式'},
                        {field: 'addr', title: '集合地'},
                        {field: 'route', title: '路线'},
                        {field:'status', title: '状态'
                            ,templet: function(d){
                                if (d.status == 0)
                                    return "未审核"
                                else
                                    return "已审核"
                            }
                        },
                        {align: 'center', toolbar: '#tableBar', title: '操作', unresize: true, width: 200}
                    ]
                ],
                id: 'pageReload',
                skin: 'line',
                size: 'lg'
            });
        });

        // 添加按钮点击事件
        $('#articleBtnAdd').click(function () {
            showAddModel();
        });

        // 工具条点击事件
        table.on('tool(articleTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') { // 删除
                layer.confirm('确定删除此数据吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('forum/delete/' + obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 200) {
                            layer.msg(data.msg, {icon: 1});
                            obj.del();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    }, 'DELETE');
                });
            } else if (layEvent === 'edit') { // 修改
                layer.confirm('确定审核此数据吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('forum/check/' + obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 200) {
                            layer.msg(data.msg, {icon: 1},function () {
                                window.location.href="/forum/toPage"
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2},function () {
                                window.location.href="/forum/toPage"
                            });
                        }
                    }, 'POST');
                });
            }
        });

        // 显示表单弹窗
        function showEditModel(data) {
            layer.open({
                type: 2,
                area: ['400px','400px'],
                title: '更新用户',
                content: base_server+'forum/check?id='+data.id
            });
        }

        function showAddModel() {
            layer.open({
                type: 2,
                area: ['400px','400px'],
                title: '添加用户',
                content: base_server+'user/toAddPage'
            });
        }
    });
</script>
</html>